package com.aiti.lulian.controller.crad;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.constant.MessageCodeConstant;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.CardInquiryDto;
import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import com.aiti.lulian.entity.LuLianEnterpriseUser;
import com.aiti.lulian.entity.crad.CardInquiry;
import com.aiti.lulian.feign.CloudShowManagerSubService;
import com.aiti.lulian.service.ICardInquiryService;
import com.aiti.lulian.service.ILuLianEnterpriseUserService;
import com.aiti.lulian.vo.CardInquiryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 询盘信息
 * @author SangGuangCan
 * @date 2024-09-21
 */
@RestController
@RequestMapping("card_inquiry")
public class CardInquiryController extends WebController {

    private ICardInquiryService cardInquiryService;

    private CloudShowManagerSubService cloudShowManagerSubService;

    @Autowired
    private ILuLianEnterpriseUserService enterpriseUserService;

    public CardInquiryController(ICardInquiryService cardInquiryService,CloudShowManagerSubService cloudShowManagerSubService) {
        this.cardInquiryService = cardInquiryService;
        this.cloudShowManagerSubService = cloudShowManagerSubService;
    }

    /**
     * 查询询盘的分页
     * @param dto 前端传来的分页数据
     * @return 分页后的数据
     */
    @GetMapping("/getInquiryPage")
    public Message getInquiryPage(CardInquiryDto dto){
        String userId = findUserId();
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        Page<CardInquiry> page = startPage(pageNum,pageSize);
        Page p = cardInquiryService.getInquiryPage(page,dto,userId);
        return Message.success(p);
    }

    /**
     * 查询当前用户的询盆列表
     * @param dto
     * @return
     */
    @GetMapping("/getInquiryList")
    public Message getInquiryList(CardInquiryDto dto){
        Page<CardInquiry> page = startPage();
        List<CardInquiryVo> list = cardInquiryService.getInquiryList(page, dto);
        PageResult result = getPageResult(list, dto.getPageNum(), dto.getPageSize(), (int)page.getTotal());
        return Message.success(result);
    }

    /**
     * 查询询盆的详情信息
     * @param cardInquiry
     * @return
     */
    @GetMapping("/getInquiryDetailInfo")
    public Message getInquiryDetailInfo(CardInquiry cardInquiry){
        if(StringUtils.isBlank(cardInquiry.getBaseId())){
            return Message.fail("没有找到主键ID");
        }
        CardInquiry inquiryDetail = cardInquiryService.getInquiryDetailInfo(cardInquiry);
        return Message.success(inquiryDetail);
    }

    /**
     * 咨询企业
     */
    @PostMapping("/addInquiryEnterprise")
    public Message addInquiryEnterprise(@RequestBody CardInquiry cardInquiry){
        if(cardInquiry == null){
            return Message.fail("参数不能为空");
        }
        cardInquiry.setType("1");
        if(StringUtils.isNotBlank(cardInquiry.getCradUserId())){
            //如查不是空需要将这个ID放到负责人ID上
            cardInquiry.setDirector(cardInquiry.getCradUserId());
            //查询当前用户所邦定的企业
            LuLianEnterpriseUser enterpriseUser = enterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", cardInquiry.getCradUserId()));
            if(enterpriseUser == null){
                throw new RuntimeException("没有找到该用户绑定的企业");
            }
            cardInquiry.setEnterpriseId(enterpriseUser.getEnterpriseId());
            //同样查询出这个用户的部门
            Message deptIdByUserId = cloudShowManagerSubService.getDeptIdByUserId(cardInquiry.getCradUserId());
            if(deptIdByUserId.getCode().equals(MessageCodeConstant.MSG_FAIL_CODE)){
                return Message.fail("获取用户部门失败");
            }
            String deptID = (String)deptIdByUserId.getObj();
            cardInquiry.setDeptId(deptID);
        }
        //设置创建时间为当前时间
        cardInquiry.setBaseCreateTime(new Date());
        cardInquiryService.save(cardInquiry);
        return Message.success("咨询成功");
    }


    /**
     * 咨询产品
     */
    @PostMapping("/addInquiryProduct")
    public Message addInquiryProduct(@RequestBody CardInquiry cardInquiry){
        if(cardInquiry == null){
            return Message.fail("参数不能为空");
        }
        cardInquiry.setType("0");
        //设置创建时间为当前时间
        cardInquiry.setBaseCreateTime(new Date());
        cardInquiryService.save(cardInquiry);
        return Message.success("咨询成功");
    }

    @PostMapping("/addWebsite")
    public Message addWebsite(@RequestBody CardInquiry cardInquiry){
        if(cardInquiry == null){
            return Message.fail("参数不能为空");
        }
//        if(StringUtils.isBlank(cardInquiry.getEnterpriseId())){
//            cardInquiry.setEnterpriseId("000b4b09-8dc7-4680-95df-5085fc32dbd0");
//        }
        cardInquiry.setType("1");
        cardInquiry.setSource("2");
        cardInquiry.setCradUserId("0");//0为无名片用户id
        cardInquiry.setCardConsultantsId("0");
        cardInquiry.setFollowUpStatus("0");
        cardInquiry.setBaseCreateTime(new Date()); //设置创建时间为当前时间
        cardInquiryService.save(cardInquiry);
        return Message.success("添加成功");
    }

    /**
     * 助企热线留言
     * @param cardInquiry
     * @return
     */
    @PostMapping("/hotLineMessage")
    public Message hotLineMessage(@RequestBody CardInquiry cardInquiry){
        if(cardInquiry == null){
            return Message.fail("参数不能为空");
        }
        cardInquiry.setType("1");
        if(StringUtils.isNotBlank(cardInquiry.getCradUserId())){
            //如查不是空需要将这个ID放到负责人ID上
            cardInquiry.setDirector(cardInquiry.getCradUserId());
            //查询被留言人的政府部门信息
            LuLianEnterpriseUser enterpriseUser = enterpriseUserService.getOne(new QueryWrapper<LuLianEnterpriseUser>().eq("user_id", cardInquiry.getCradUserId()));
            if(enterpriseUser == null){
                throw new RuntimeException("没有找到被留言人所属的政府部门");
            }
            cardInquiry.setEnterpriseId(enterpriseUser.getEnterpriseId());

            Message deptIdByUserId = cloudShowManagerSubService.getDeptIdByUserId(cardInquiry.getCradUserId());
            if(deptIdByUserId.getCode().equals(MessageCodeConstant.MSG_FAIL_CODE)){
                return Message.fail("获取被留言人所属的部门失败");
            }
            String deptID = (String)deptIdByUserId.getObj();
            cardInquiry.setDeptId(deptID);
        }
        //设置创建时间为当前时间
        cardInquiry.setBaseCreateTime(new Date());
        cardInquiryService.save(cardInquiry);
        return Message.success("留言成功");
    }
}
